{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "305aecb9",
   "metadata": {},
   "source": [
    "# 语音录制  \n",
    "* 安装与下载模块：pip install;win+R    \n",
    "> 1.SpeechRecognition（语音识别）  \n",
    "> 2.Pyaudio  \n",
    "> 3.pydub（播放）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "4343c7d0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 录制\n",
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "54c12a8a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ASR自动语音识别  auto speech recognition"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55f19f33",
   "metadata": {},
   "source": [
    "# 语音识别  \n",
    "* I/O 音频文件--> 文本文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "af16407d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import bdasr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "6ea2bb9d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Request time cost 0.960174\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'{\"corpus_no\":\"7244909483657994240\",\"err_msg\":\"success.\",\"err_no\":0,\"result\":[\"你是谁？\"],\"sn\":\"350613213251686836938\"}\\n'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 识别语音\n",
    "bdasr_result_1 = bdasr.asr(\"/api/week13成功版！/1.wav\")\n",
    "bdasr_result_1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "0ab3e467",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'你是谁？'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 提取结果\n",
    "bdasr_result = eval(bdasr_result_1)[\"result\"][0]\n",
    "bdasr_result"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "db8af734",
   "metadata": {},
   "source": [
    "# 自然语言处理（NLP）  \n",
    "* "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "c271cc16",
   "metadata": {},
   "outputs": [],
   "source": [
    "qa = {\n",
    "    \"你是谁？\":\"我是小度\",\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "c004df19",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'我是小度'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "qa_result = qa.get(bdasr_result)\n",
    "qa_result"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "05adafc6",
   "metadata": {},
   "source": [
    "# 语音合成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "f865ab6d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 有问题换key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "63a739e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import bdtts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "ec6780e8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "fetch token begin\n",
      "{\"refresh_token\":\"25.96d2f810aeaa8cff54da979922c4c9a2.315360000.2002196939.282335-34863876\",\"expires_in\":2592000,\"session_key\":\"9mzdDxYJMFEEyKUIvUE7J6\\/QoAT2YjRd+oow8oDxalgNUr90+B61r+uomMEYEoaj25pzky9bw2qj3IwZmbklrl2P58T3dQ==\",\"access_token\":\"24.4266047a33f459fe953cd2470af402c3.2592000.1689428939.282335-34863876\",\"scope\":\"audio_voice_assistant_get brain_enhanced_asr audio_tts_post brain_speech_realtime public brain_all_scope brain_asr_async wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test\\u6743\\u9650 vis-classify_flower lpq_\\u5f00\\u653e cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_\\u5f00\\u653eScope vis-ocr_\\u865a\\u62df\\u4eba\\u7269\\u52a9\\u7406 idl-video_\\u865a\\u62df\\u4eba\\u7269\\u52a9\\u7406 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online smartapp_gov_aladin_to_xcx\",\"session_secret\":\"7291b7285cea493c0923ac3b4c789161\"}\n",
      "\n",
      "{'refresh_token': '25.96d2f810aeaa8cff54da979922c4c9a2.315360000.2002196939.282335-34863876', 'expires_in': 2592000, 'session_key': '9mzdDxYJMFEEyKUIvUE7J6/QoAT2YjRd+oow8oDxalgNUr90+B61r+uomMEYEoaj25pzky9bw2qj3IwZmbklrl2P58T3dQ==', 'access_token': '24.4266047a33f459fe953cd2470af402c3.2592000.1689428939.282335-34863876', 'scope': 'audio_voice_assistant_get brain_enhanced_asr audio_tts_post brain_speech_realtime public brain_all_scope brain_asr_async wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online smartapp_gov_aladin_to_xcx', 'session_secret': '7291b7285cea493c0923ac3b4c789161'}\n",
      "SUCCESS WITH TOKEN: 24.4266047a33f459fe953cd2470af402c3.2592000.1689428939.282335-34863876 ; EXPIRES IN SECONDS: 2592000\n",
      "%E6%88%91%E6%98%AF%E5%B0%8F%E5%BA%A6\n",
      "test on Web Browserhttp://tsn.baidu.com/text2audio?tok=24.4266047a33f459fe953cd2470af402c3.2592000.1689428939.282335-34863876&tex=%25E6%2588%2591%25E6%2598%25AF%25E5%25B0%258F%25E5%25BA%25A6&per=4&spd=5&pit=5&vol=5&aue=6&cuid=123456PYTHON&lan=zh&ctp=1\n",
      "tts api  error:{\"err_detail\":\"16: Open api characters limit reached\",\"err_msg\":\"16: Open api characters limit reached\",\"err_no\":502,\"err_subcode\":16,\"tts_logid\":2228389777}\n",
      "\n",
      "result saved as :error.txt\n"
     ]
    }
   ],
   "source": [
    "bdtts.tts(qa_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8aea16e5",
   "metadata": {},
   "source": [
    "# 连接openai"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "f4191f25",
   "metadata": {},
   "outputs": [],
   "source": [
    "import opchat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6547ae23",
   "metadata": {
    "pycharm": {
     "is_executing": true
    }
   },
   "outputs": [],
   "source": [
    "result = opchat.openai_chat(bdasr_result)\n",
    "result"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae510ea2",
   "metadata": {},
   "source": [
    "# 语音播放"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c68fe461",
   "metadata": {
    "pycharm": {
     "is_executing": true
    }
   },
   "outputs": [],
   "source": [
    "from pydub import AudioSegment\n",
    "from pydub.playback import play\n",
    "song = AudioSegment.from_wav('result.wav')\n",
    "play(song)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "481bc18d",
   "metadata": {
    "pycharm": {
     "is_executing": true
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c821ad00",
   "metadata": {
    "pycharm": {
     "is_executing": true
    }
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.0"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
